package com.telecom.cms.util;     

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Enumeration;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
                                                                                                                                                                       
/**
 * ExportServlet.java
 * 导出文件类，默认导出成EXCEL
 * 将JSP中的TABLE内容导出到EXCEL文件或者打开，或其它
 *
 * @author ylpeng
 * @date Apr 24, 2007
 */                                                                                  
public class ExportUtil extends HttpServlet {                                      
   private static final String CONTENT_TYPE = "text/html; charset=utf-8";            
   private static final String TO_GBENCODE = "utf-8";                               
   //Initialize global variables                                                      
   public void init() throws ServletException {   
	   
   }                                                                                  
                                                                                      
   //Process the HTTP Get request                                                     
   public void doGet(HttpServletRequest request, HttpServletResponse response) throws 
       ServletException, IOException {                                                
      response.setContentType(CONTENT_TYPE);                                          
      String file = null;                                                             
      StringBuffer sb = null;                                     
      
      boolean isNeedConvert = true;                                             
      String orgEncode = request.getCharacterEncoding();                   
                                                                                      
      if( orgEncode==null )
    	  orgEncode = "ISO8859_1";                             
      if(orgEncode.equalsIgnoreCase(TO_GBENCODE))
    	  isNeedConvert = false;         
                                                                                      
      for (Enumeration e = request.getParameterNames(); e.hasMoreElements(); ) {      
         String name = (String) e.nextElement(); 
         if (name.equals("exportFileName")) {                                         
            file = request.getParameter(name);        
         }                                                                            
         if (name.equals("exportContent")) {                                          
            sb = new StringBuffer(request.getParameter(name));                        
         }                                                                            
      }                                                                               
                                                                                      
      try {                                                                           
         if(isNeedConvert){                                                           
            file = new String(file.getBytes(orgEncode), TO_GBENCODE);                 
         }                                  
      }                                                                               
      catch (UnsupportedEncodingException e) {                                        
         System.out.println("文件名解析失败！");                                      
         return;                                                                      
      }                                                                               
                                                                                      
      String contentType;                                                             
      String extName = file.substring(file.lastIndexOf("."));                         
      if (extName == null || extName.equals("")) {                                    
         contentType = "";                                                            
      }                                                                               
      else if (extName.equalsIgnoreCase(".chm")) {                                    
         contentType = "application/msword";                                          
      }                                                                               
      else if (extName.equalsIgnoreCase(".doc")) {                                    
         contentType = "application/msword";                                          
      }                                                                               
      else if (extName.equalsIgnoreCase(".xls")) {                                    
         contentType = "application/vnd.ms-excel";                                    
      }                                                                               
      else if (extName.equalsIgnoreCase(".ppt")) {                                    
         contentType = "application/vnd.ms-powerpoint";                               
      }                                                                               
      else if (extName.equalsIgnoreCase(".txt")) {                                    
         contentType = "text/plain";                                                  
      }                                                                               
      else {                                                                          
         contentType = "application/octet-stream";                                    
                                                                                      
      }                                                                               
                                                                                      
      try {                                                                           
         String recommendedName = new String(file.getBytes(), orgEncode);             
         //String recommendedName =  new String(file);                                
         response.reset();
         //设定输出的网页为excel格式的网页
         response.setContentType(contentType);                                        
         //设置页面不缓存                                                             
         response.setHeader("Pragma", "No-cache");                                    
         response.setHeader("Cache-Control", "no-cache");                             
         response.setDateHeader("Expires", 0);                                        
         
         //response.setHeader("Content-disposition","inline; filename=" + recommendedName);
         response.setHeader("Content-Disposition","attachment; filename=" + recommendedName + "\"");
         //设定传送到前端浏览器时的档名为recommendedName，就是靠这一行，让前端浏览器以为接收到一个excel档 
                                                                                      
         response.resetBuffer();                                                      
         ServletOutputStream sos = response.getOutputStream();                        
         sos.write(sb.toString().getBytes("gb2312"));                                
         sos.flush();                                                                 
         sos.close();                                                                 
      }                                                                               
      catch (Exception e) {                                                           
         System.out.println("数据导出失败！" + e.getMessage());                       
         return;                                                                      
      }                                                                               
   }                                                                                  
                                                                                      
   //Process the HTTP Post request                                                    
   public void doPost(HttpServletRequest request, HttpServletResponse response) throws
       ServletException, IOException {                                       
      doGet(request, response);                                                       
   }                                                                                  
                                                                                      
   //Clean up resources                                                               
   public void destroy() {
	   
   }                                                                                  
}                                                                                     